<?php

/**
 * Model MyARContent
 *
 * @author luubk
 * @since Jan 17, 2013 - 3:29:35 PM
 */
class MyARContent extends MyAR
{

    const STATUS_PUBLIC = 1;
    const STATUS_DRAFT = 0;
    const STATUS_HIGHLIGHT = 2;

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        $table = $this->getTableName();
        return array(
            'created' => array(
                self::HAS_ONE,
                'Activity',
                'object_id',
                'condition' => "type='create' AND object_type='$table'",
            ),
            'updated' => array(
                self::HAS_ONE,
                'Activity',
                'object_id',
                'condition' => "type='update' AND object_type='$table'",
            ),
            'language' => array(
                self::BELONGS_TO,
                'Language',
                'language_id',
                'condition' => "status!=0",
            ),
        );
    }

    public function beforeSave()
    {
        if (parent::beforeSave())
        {
            if ($this->hasAttribute("language_id"))
                $this->language_id = Yii::app()->controller->lang_id;
            return TRUE;
        }
        else
            return FALSE;
    }

    public static function getArrayStatus()
    {
        return array(
            self::STATUS_DRAFT => Yii::t('myarcontent', 'Draft'),
            self::STATUS_PUBLIC => Yii::t('myarcontent', 'Public'),
            self::STATUS_HIGHLIGHT => Yii::t('myarcontent', 'Highlight'),
        );
    }

    public function getStatusLabel()
    {
        if (!$this->hasAttribute("status"))
            return '';

        $labelClass = 'label ';
        switch ($this->status)
        {
            case self::STATUS_PUBLIC:
                $labelClass .= 'label-info';
                break;

            case self::STATUS_HIGHLIGHT:
                $labelClass .= 'label-success';
                break;

            default:
                break;
        }
        $labels = self::getArrayStatus();
        $label = isset($labels[$this->status]) ? $labels[$this->status] : $this->status;
        return "<span class='$labelClass'>$label</span>";
    }

    public function getPublicUrl()
    {
        return '';
    }

    public function getUrlContact()
    {
        return Yii::app()->createUrl('site/contact', array(
                    'lang' => $this->language->code));
    }

    /* Ham Crop Anh Theo kick Thuoc Dinh San */

    public function resizePhoto($img, $width, $height)
    {
        $original_dir = 'files';
        $output_dir = 'cache';

        $original_path = Yii::app()->baseUrl . "/$original_dir/";
        $base_image_path = str_replace($original_path, "", $img);

        $original_file = "$original_dir/$base_image_path";
        if (!file_exists($original_file) OR $img == NULL)
        {
            $base_image_path = 'no_thumbnail.png';
            $original_file = "$original_dir/$base_image_path";
        }

        $output_file = "$output_dir/{$width}x{$height}/$base_image_path";

        // Neu chua ton tai forder thi tao
        if (!is_dir(dirname($output_file)))
        {
            @mkdir(dirname($output_file), 0777, true);
        }

        // Neu Khong ton tai anh thi crop anh
        if (!file_exists($output_file))
        {
            $thumb = Yii::app()->phpThumb->create($original_file);
            $thumb->adaptiveResize($width, $height);
            $thumb->save($output_file);
        }
        return Yii::app()->baseUrl . "/" . $output_file;
    }

    /**
     * Post bài lên facebook fanpage
     * Vào trang quản lý fanpage để lấy những thông tin cần thiết và config file config/params.php
     * @param string $msg
     * @param string $link absolute url
     * @param string $picture absolute url
     * @param string $title
     * @param string $website
     * @param type $description
     */
    public static function postFacebook($msg, $link, $picture, $title, $website, $description)
    {
        $access_token = Yii::app()->params['facebook']['access_token'];
        $fb_id = Yii::app()->params['facebook']['fb_id'];
        //Thu vien Facebook SDK
        Yii::import('application.extensions.facebook.Facebook');
        $fb = new Facebook(Yii::app()->params['facebook']);//Config
//        $fb->getExtendedAccessToken();
        $data = array(
            "access_token" => $access_token,
            "message" => $msg,
            "link" => $link,
            "picture" => $picture,
            "name" => $title,
            "caption" => $website,
            "description" => $description
        );
        print_R($data);die;

        try
        {
            $ret = $fb->api("/$fb_id/feed", 'POST', $data);
                die('tt');
            return TRUE;
        }
        catch (Exception $e)
        {
            echo $e->getMessage();
        }
    }
}

/** 
 * End file MyARContent
 */
